﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using tmAsk.Data;

namespace tmASK.Collect
{
    class baidu
    {
        public string collect_url = "http://zhidao.baidu.com/q?ct=17&tn=ikaslist";
        private MakehtmlSQL Sql = new MakehtmlSQL();

        public void Collect_class(string url, string From_page, string Topage)
        {
            if ((From_page != null) && (Topage != null))
            {
                for (int i = Convert.ToInt16(From_page); i <= Convert.ToInt16(Topage); i++)
                {
                    string str = GetPage(string.Concat(new object[] { "http://zhidao.baidu.com/browse/", url, "?lm=0&word=&pn=", (i - 1) * 0x19 })).Replace("\"", "");
                    string oldValue = "<span class=f14><a href=/question/";
                    string[] strArray = str.Replace(oldValue, "△").Split(new char[] { '△' });
                    string str4 = "";
                    for (int j = 1; j < strArray.Length; j++)
                    {
                        str4 = strArray[j];
                        int index = str4.IndexOf("title=");
                        int num4 = str4.IndexOf(" target", index);
                        string title = str4.Substring(index + 6, num4 - (index + 6));
                        int length = str4.IndexOf(".html");
                        string str6 = "http://zhidao.baidu.com/question/" + str4.Substring(0, length) + ".html";
                        this.Colled(str6, title, url);
                    }
                }
            }
        }

        public void Collect_list(string word, string From_page, string Topage)
        {
            if ((From_page != null) && (Topage != null))
            {
                for (int i = Convert.ToInt16(From_page); i <= Convert.ToInt16(Topage); i++)
                {
                    string page = GetPage(string.Concat(new object[] { this.collect_url, "&rn=10&word=", word, "&lm=0&pn=", (i - 1) * 10 }));
                    string oldValue = "<table border=0 cellpadding=0 cellspacing=0><tr><td class=f>";
                    string[] strArray = page.Replace(oldValue, "△").Split(new char[] { '△' });
                    string str3 = "";
                    string str4 = "";
                    for (int j = 1; j < strArray.Length; j++)
                    {
                        if (j == (strArray.Length - 1))
                        {
                            int length = strArray[j].IndexOf("<br />");
                            str4 = strArray[j].Substring(0, length);
                        }
                        else
                        {
                            str4 = strArray[j];
                        }
                        str3 = str4.Replace("\"", "");
                        int index = str3.IndexOf("_blank>");
                        int num5 = str3.IndexOf("</a>");
                        string title = str3.Substring(index + 7, num5 - (index + 7));
                        int num6 = str3.IndexOf("href=");
                        int num7 = str3.IndexOf("target");
                        string url = "http://zhidao.baidu.com" + str3.Substring(num6 + 5, num7 - (num6 + 5));
                        this.Colled(url, title, word);
                    }
                }
            }
        }

        private void Colled(string url, string title, string key)
        {
            int num = 0;
            try
            {
                string page = GetPage(url);
                string str2 = "";
                string str3 = "";
                string str4 = "";
                page = page.Replace("\"", "");
                string str5 = "<pre id=question-content>";
                int index = page.IndexOf(str5);
                int num3 = 0;
                if (index > 0)
                {
                    num3 = page.IndexOf("</pre>", index);
                    str2 = HttpContext.Current.Server.HtmlEncode(page.Substring(index, num3 - index).Replace(str5, "")).Replace("'", "").Replace("&lt;", "<").Replace("&gt;", ">");
                }
                string str6 = "<pre id=best-answer-content class=reply-text mb10>";
                int startIndex = page.IndexOf(str6);
                int num5 = 0;
                if (startIndex > 0)
                {
                    num5 = page.IndexOf("</pre>", startIndex);
                    str3 = HttpContext.Current.Server.HtmlEncode(page.Substring(startIndex, num5 - startIndex).Replace(str6, "")).Replace("'", "");
                }
                string str7 = "解决时间：";
                int num6 = page.IndexOf(str7);
                int num7 = 0;
                if (num6 > 0)
                {
                    num7 = page.IndexOf("</span>", num6);
                    str4 = page.Substring(num6, num7 - num6).Replace(str7, "");
                }
                string str8 = "insert into B_api_data (A_title,A_content,A_reply,A_replyTime,A_addtime,A_comeUrl,A_type,A_key)values";
                string str9 = "('" + NoHTML(title) + "','" + str2 + "','" + str3 + "','" + str4 + "','" + DateTime.Now.ToString() + "','" + url + "','百度知道','" + key + "')";
                if (!string.IsNullOrEmpty(str3))
                {
                    num = this.Sql.ExecuteSql(str8 + str9);
                }
            }
            catch
            {
            }
        }

        private static string GetPage(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
            string str = reader.ReadToEnd();
            reader.Close();
            response.Close();
            return str.Replace("<script", "&lt;script");
        }

        private static string GetPageUTF(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
            string str = reader.ReadToEnd();
            reader.Close();
            response.Close();
            return str.Replace("<script", "&lt;script");
        }

        public static string NoHTML(string Htmlstring)
        {
            Htmlstring = Regex.Replace(Htmlstring, "<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "&(iexcl|#161);", "\x00a1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "&(cent|#162);", "\x00a2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "&(pound|#163);", "\x00a3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "&(copy|#169);", "\x00a9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
            return Htmlstring;
        }
    }
}
